#ifndef __USER_DEFINE_H_
#define __USER_DEFINE_H_

#ifdef __cplusplus
extern "C"{
#endif

#include "basic.h"
#include "customer.h" 

/* 载波峰值 */
#define PWM_PERIOD                     ((u16) (PWM_MCLK / (u32)(2 * PWM_FREQ *(PWM_PRSC+1))))
/*rtt示波器功能*/
#define RTT_ON 1
#define RTT_OFF 0
#define RTT_FUNCTION  RTT_OFF//RTT_ON//RTT_ON //
//debug
#define DEBUG_ENABLE 1                                          // 调试模式使能
#if DEBUG_ENABLE == 1
    #define DEBUG_STATE
#endif
/*******************sytem pu set********************************/
#define BASE_VOLT_PU          ((float) VOLT_UDC/1.732)  //Unit:V
#define BASE_CURRENT_PU       ((float) HAREWARE_IMAX/2.0) //Unit:A
#define BASE_SPEED_PU         ((float) MOTOR_POLEPAIRS*MAX_SPEED_LIMIT/60.0)
#define BASE_POWER_PU         ((float) 1.5*BASE_VOLT_PU*BASE_CURRENT_PU)
//定时器参数
#define MIDDLE_TASK_TIMER_BASE   2     //500us计数基值
#define MID_FREQ ((u16) 1000/MIDDLE_FREQUENCY_TASK_MS)

#define MIDDLE_FREQUENCY_TASK_PERIOD ((u16)(MIDDLE_TASK_TIMER_BASE*MIDDLE_FREQUENCY_TASK_MS)) //中频任务定时周期计数
#define LOW_FREQUENCY_TASK_PERIOD    ((u16)(MIDDLE_TASK_TIMER_BASE*LOW_FREQUENCY_TASK_MS))
//计算系数
#if(HARDWARE_UDC_SAMPLE_ENABLE == 1)
#else
#define VOLTAGE_COEFFICIENT  ((float)1.0)          // q11==3.52
#endif
#define VELOCITY_COEFFICIENT ((s16) ((float)2.0*BASE_SPEED_PU/PWM_FREQ*32767))//速度系数
#define POWER_COEFFICIENT    ((s16)((float)MAX_POWER/BASE_POWER_PU*16384))                                    //功率系数

#if(CONTROL_MODE==0)
#define LEVEL1_SPEED_CMD ((s16)((float)LEVEL1_COMMAND*16384))  //1档速度指令
#define LEVEL2_SPEED_CMD ((s16)((float)LEVEL2_COMMAND*16384))  //2档速度指令
#define LEVEL3_SPEED_CMD ((s16)((float)LEVEL3_COMMAND*16384))  //3档速度指令
#define LEVEL4_SPEED_CMD ((s16)((float)LEVEL4_COMMAND*16384))  //4档速度指令
#define LEVEL5_SPEED_CMD ((s16)((float)LEVEL5_COMMAND*16384))  //5档速度指令
#define LEVEL6_SPEED_CMD ((s16)((float)LEVEL6_COMMAND*16384))  //6档速度指令
#define DEFAULT_VELOCITY_REFERENCE (LEVEL1_SPEED_CMD)        //默认速度指令
#else
#define LEVEL1_POWER_CMD ((s16)((float)LEVEL1_COMMAND*POWER_COEFFICIENT))  //1档功率指令
#define LEVEL2_POWER_CMD ((s16)((float)LEVEL2_COMMAND*POWER_COEFFICIENT))  //2档功率指令
#define LEVEL3_POWER_CMD ((s16)((float)LEVEL3_COMMAND*POWER_COEFFICIENT))  //3档功率指令
#define LEVEL4_POWER_CMD ((s16)((float)LEVEL4_COMMAND*POWER_COEFFICIENT))  //4档功率指令
#define LEVEL5_POWER_CMD ((s16)((float)LEVEL5_COMMAND*POWER_COEFFICIENT))  //5档功率指令
#define LEVEL6_POWER_CMD ((s16)((float)LEVEL6_COMMAND*POWER_COEFFICIENT))  //6档功率指令
#define DEFAULT_VELOCITY_REFERENCE (LEVEL1_POWER_CMD)        //默认速度指令
#endif


// adc校准
#define CALIB_MEASURE_TIMES_POWER 4                //  adc校准次数2^4 = 16
#define CALIB_OFFSET_MAX 3520                      // 偏置超限故障阈值 3520 -> 3520/32767*10.24A = 1.1A
#define TRIGER01_OFFSET  (-200)                    //单电阻采样02偏置校准
// 充电
#define BOOT_CAP_CHARGE_TIME (1)                   //充电时间：=n*中频任务时间 
#define BOOT_CAP_CHARGE_DUTY 24000                  //充电占空比 9830/32768 = 30%
// SVPWM配置
#define PWM_CLOCK_DIVSION EPWM_CLK_DIV1
#define PWM_OVER_MODULE_RATE   16383  //16080         //过调制电压限制
#define PWM_OVER_MODULE_SATMAX 15236                //最大调制电压限制0.85*16384

#define PWM_ADC_SAMPLE_DELAY       ((s16)((float)SAMPLE_WINDOW*PWM_FREQ*2.0/1000000*32767))//PWM ADC采样窗口
#define PWM_ADC_SAMPLE_OFFSET      ((s16)((float)SAMPLE_01_OFFSET*PWM_FREQ*2.0/1000000*32767))//300//30 //1200         // PWM ADC偏置延迟
#define PWM_ADC_SAMPLE_OFFSET_01   ((s16)((float)SAMPLE_02_OFFSET*PWM_FREQ*2.0/1000000*32767))//300//30 //1200         // PWM ADC偏置延迟

#define POLARITY_POSITIVE   0   // PWM极性，高电平有效
#define POLARITY_NEGATIVE   1   // PWM极性，低电平有效
#define HIGH_SIDE_POLARITY  (POLARITY_POSITIVE  )   // （*）高侧极性为高电平有效
#define LOW_SIDE_POLARITY   (POLARITY_POSITIVE  )   // （*）低侧极性为低电平有效
#define PWM_DEAD_TIME ((u16)((float)PWM_DEATIME_US*PWM_FREQ*2*PWM_PERIOD/1000000))
	
#define PWM_SWITCH_DELAY_TIME ((u16)((float)0.5*PWM_FREQ*2*PWM_PERIOD/1000000))  //开通延迟0.5us，用于IPD续流

#if HIGH_SIDE_POLARITY == POLARITY_POSITIVE
    #define PWM_H_ON    0x0000000A
    #define PWM_H_OFF   0x00000002
#else
    #define PWM_H_ON    0x00000002
    #define PWM_H_OFF   0x0000000A
#endif

#if LOW_SIDE_POLARITY == POLARITY_POSITIVE
    #define PWM_L_ON    0x00000005
    #define PWM_L_OFF   0x00000001
#else
    #define PWM_L_ON    0x00000001
    #define PWM_L_OFF   0x00000005
#endif

#define PWM_H_OUT 0x00000000
#define PWM_L_OUT 0x00000000

#define PWM_FORCE_CH0 0
#define PWM_FORCE_CH1 4
#define PWM_FORCE_CH2 8

#define PWM_FLY       (0x333)//(0xBBB)
#define PMW_BRAKE     (0x777)
#define PWM_RELEASE   (0x000)

// 运行参数
#define RUN_ACC   ((s16)(float)DEFAULT_VELOCITY_REFERENCE*MIDDLE_FREQUENCY_TASK_MS/RUN_ACC_TIME_MS)
#define RUN_DEC   ((s16)RUN_ACC>>1) 

#define RUN_VELO_MIN ((s16)((float)RUN_VELO_MIN_RATIO*16384))   //反向到顺逆风切换速度阈值
#define RUN_IQ_MAX   ((s16)((float)RUN_IQ_MAX_RATIO*16384))     //gain32: 8000观测器低速下电流限制
#define RUN_IQ_MIN   ((s16)((float)RUN_IQ_MIN_RATIO*16384)) 

#define RUN_REVERSE_BRAKE_TIME  ((u16)((float)REVERSE_BRAKE_TIME/MIDDLE_FREQUENCY_TASK_MS)) //增益切换滤波时间

#define RUN_CORRECT_TIMES ((u16)((float)OBSERVER_GAIN_SWITCH_TIME*PWM_FREQ/1000)) //增益切换滤波时间
#define RUN_VELO_BOUNDARY (RUN_VELO_MIN)         //增益切换速度阈值

#define ON_THE_FLY_DETECT_TIME       ((u16)((float)ON_THE_FLY_DETECT_TIME_MS/MIDDLE_FREQUENCY_TASK_MS))    // 顺逆风检测时间
#define ON_THE_FLY_DETECT_CURRENT    ((s16)((float)ON_THE_FLY_DETECT_CUR_RATIO*16384))                           // 顺逆风检测电流
#define ON_THE_FLY_BRAKE_CURRENT     (ON_THE_FLY_DETECT_CURRENT)                          // 顺逆风刹车电流
#define ON_THE_FLY_CONTINUE_CORRECT  (ON_THE_FLY_CONFIRM_CNT)                            // 顺逆风检测次数
#define ON_THE_FLY_VELOCITY_BOUNDARY (DEFAULT_VELOCITY_REFERENCE - 1000)                 //顺风切换阈值
  
#define STOP_BRAKE_VELOCITY 100                                 //停机速度阈值
#define STOP_BRAKE_TIME 100                                       // 停机时间
 
//滑模观测器

#define VELO_SENSOR_SMO_KSW     _IQfvalue(1.0,14)                        //q14固定
#define VELO_SENSOR_SMO_SATMAX  PWM_OVER_MODULE_RATE                  // 滑动模态电压限制
#define VELO_SENSOR_SMO_SATMIN  (-PWM_OVER_MODULE_RATE)    
                
#define VELO_SENSOR_SMO_PLL_SATMAX SMO_PLL_SATMAX                   // 
#define VELO_SENSOR_SMO_PLL_SATMIN SMO_PLL_SATMIN                      //

#define VELO_PLL_PERIOD  16//小扇叶0 大扇叶16   //锁相环计算周期（基于中断），调试大惯量扇叶需调试此参数，范围0-100
#define OB_GAIN         16383  //观测器自适应增益，一般无需调整，范围0到32767

#define FORCE_DRIVE_FLAG   0  //用于使用IF强拖和切换，可能修改状态机，增加强拖状态
// 电流环 

#define TORQUE_PI_CONTROLLER_SAT_MAX PWM_OVER_MODULE_RATE
#define TORQUE_PI_CONTROLLER_SAT_MIN (-PWM_OVER_MODULE_RATE) 

#define FLUX_PI_CONTROLLER_SAT_MAX  0   //d轴电压限幅
#define FLUX_PI_CONTROLLER_SAT_MIN  (-PWM_OVER_MODULE_RATE)
// 速度环

#define VELOCITY_PI_CONTROLLER_KP   ((float)VELOCITY_PI_KP)        
#define VELOCITY_PI_CONTROLLER_KI   ((float)VELOCITY_PI_KI)       
#define VELOCITY_PI_CONTROLLER_SAT_MAX        _IQfvalue(MAX_CURRENT_LIMIT/BASE_CURRENT_PU,14)  //gain32: 24000//
#define VELOCITY_PI_CONTROLLER_SAT_MIN        (RUN_IQ_MIN) //important for stall
// 弱磁环

#define FLUX_WEAKENING_PI_CONTROLLER_KP 0.0
#define FLUX_WEAKENING_PI_CONTROLLER_KI 0.0
#define FLUX_WEAKENING_PI_CONTROLLER_SAT_MAX 8192   //最大弱磁角：8192==》45°：180°/4*32767
#define FLUX_WEAKENING_PI_CONTROLLER_SAT_MIN 0
// 功率环

#define POWER_PI_CONTROLLER_KP       ((float)POWER_PI_KP) // 
#define POWER_PI_CONTROLLER_KI       ((float)POWER_PI_KI)// 
#define POWER_PI_CONTROLLER_SAT_MAX    _IQfvalue(MAX_CURRENT_LIMIT/BASE_CURRENT_PU,14)//
#define POWER_PI_CONTROLLER_SAT_MIN    (RUN_IQ_MIN)//
//电机参数辨识
#define RESISTANCE_ESTIMATE_TIME  ((u16) 2000*MID_FREQ/1000)   // （*）电阻测量时间，单位：毫秒（ms）
#define INDUCTANCE_ESTIMATE_TIME  ((u16) 2000*MID_FREQ/1000)   // （*）电感测量时间，单位：毫秒（ms）
#define BEMF_ESTIMATE_TIME        ((u16) 10000*MID_FREQ/1000)  // （*）反电动势测量时间，单位：毫秒（ms）
#define FRICTION_ESTIMATE_TIME    ((u16) 20000*MID_FREQ/1000)  // （*）粘滞摩擦测量时间，单位：毫秒（ms）
#define INERTIA_ESTIMATE_TIME     ((u16) 20000*MID_FREQ/1000)  // （*）转动惯量测量时间，单位：毫秒（ms）

////////////////////////保护///////////////////////
#define OVERCURRENT_THRESHOLD _IQfvalue(OVER_CURRENT_THRESHOLD_RATIO,14) 
#define FAULT_DELAY_TIME ((u16)((float)FAULT_RECOVER_DELAY_TIME_MS/MIDDLE_FREQUENCY_TASK_MS))   
////////////////end/////////////////////////////////
#ifdef __cplusplus
}
#endif


#endif
